//
//  ST_VerticalButton.swift
//  btvideo

/* 【how to use】
 【位置】
 
   只需要设置imageView相对于父视图的 image_view_edges
   titleLabel 相对于父视图的 title_bottom_offset
 
 【图片】【文字】【颜色】 推荐使用
  setImageForAllStates
  setTitleForAllStates
  setTitleColorForAllStates
 
*/

//  Created by Jarvan on 2020/1/16.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

/**
 竖直排列Button
 */
class ST_VerticalButton: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(imageView)
        self.addSubview(titleLabel)
        
        imageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(image_view_edges)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(title_bottom_offset)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK:- private Properties
    /**
     当前支持两种状态就行啦
     */
    fileprivate var states: [UIControl.State] {
        return [.normal, .selected]
    }
    
    /**
     key = UIControl.State.rawValue
     */
    fileprivate var background_color_map = [UInt:UIColor]()
    
    /**
     key = UIControl.State.rawValue
     */
    fileprivate var imageMap = [UInt:UIImage]()
    
    /**
     key = UIControl.State.rawValue
     */
    fileprivate var image_url_map = [UInt:URL]()
    /**
    key = UIControl.State.rawValue
    */
    fileprivate var title_color_map = [UInt:UIColor]()
    /**
    key = UIControl.State.rawValue
    */
    fileprivate var title_text_map = [UInt:NSAttributedString]()
    
    fileprivate var titleLabel:UILabel = {
       let control = UILabel()
        return control
    }()
    
    var imageView:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.clipsToBounds = true
        return control
    }()
    
    
    //    MARK:- private Method
    fileprivate func bind_control_value(){
        
        var key = UIControl.State.normal.rawValue
        
        if isSelected{
            key = UIControl.State.selected.rawValue
        }
        
        if let url = image_url_map[key]{
            
            imageView.pin_setImage(from: url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        }else if let img = imageMap[key]{
            imageView.image = img
        }
        
        if let color = title_color_map[key]{
            titleLabel.textColor = color
        }
        
        if let color = background_color_map[key]{
            self.backgroundColor = color
        }
        
        if let attributedStr = title_text_map[key]{
            titleLabel.attributedText = attributedStr
        }
    }
    

    //    MARK:- Public Properties
    var image_view_edges = UIEdgeInsets.init(top: 1, left: (49-32)/2.0, bottom: 16, right: (49-32)/2.0){
        didSet{
            
            imageView.snp.updateConstraints { (make) in
                make.edges.equalToSuperview().inset(image_view_edges)
            }
        }
    }
    
    var title_bottom_offset = -1{
        didSet{
            titleLabel.snp.updateConstraints { (make) in
                make.bottom.equalToSuperview().offset(title_bottom_offset)
            }
        }
    }
    
    var imageViewCornerRadius = 0.cgFloat{
        didSet{
            imageView.cornerRadius = imageViewCornerRadius
        }
    }
    
    var image_view_border_width = 0.cgFloat{
        didSet{
            imageView.borderWidth = image_view_border_width
        }
    }
    
    override var isSelected: Bool{
        didSet{
            bind_control_value()
        }
    }
}

//    MARK:- public Method
extension ST_VerticalButton{
    
    func setBackgroundColor(_ color: UIColor, for state: UIControl.State){
        background_color_map.updateValue(color, forKey: state.rawValue)
        bind_control_value()
    }
    
    func setTitleColor(_ color: UIColor, for state: UIControl.State){
        title_color_map.updateValue(color, forKey: state.rawValue)
        bind_control_value()
    }

    func setImage(_ image: UIImage, for state: UIControl.State){
        imageMap.updateValue(image, forKey: state.rawValue)
        bind_control_value()
    }
    
    func setImageUrl(_ imageUrl: URL, for state: UIControl.State){
        image_url_map.updateValue(imageUrl, forKey: state.rawValue)
        bind_control_value()
    }
    
    func setTitle(_ title: NSAttributedString, for state: UIControl.State){
        title_text_map.updateValue(title, forKey: state.rawValue)
        bind_control_value()
    }

    //    MARK:- Recommend public Method
    /// Set image for all states.
    ///
    /// - Parameter image: UIImage.
    func setImageForAllStates(_ image: UIImage) {
        states.forEach { setImage(image, for: $0) }
    }
    
    /// Set image url for all states.
    ///
    /// - Parameter imageUrl: URL.
    func setImageForAllStates(_ imageUrl: URL) {
        states.forEach { setImageUrl(imageUrl, for: $0) }
    }

    /// Set title color for all states.
    ///
    /// - Parameter color: UIColor.
    func setTitleColorForAllStates(_ color: UIColor) {
        states.forEach { setTitleColor(color, for: $0) }
    }
    
    ///  Set title for all states.
    ///
    /// - Parameter title: title string.
    func setTitleForAllStates(_ title: NSAttributedString) {
        states.forEach { setTitle(title, for: $0) }
    }
    
    func add_touch_up(insideTarget target:Any,action:Selector){
        self.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
    }
}



//首页使用
class ST_BTTabBarButton: ST_VerticalButton {
    
    fileprivate let shapeLayer = CAShapeLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        //change the fill color
        shapeLayer.fillColor = UIColor.clear.cgColor
        //you can change the stroke color
        shapeLayer.strokeColor = UIColor.init(hexString: "#FEC207")!.cgColor
        //you can change the line width
        shapeLayer.lineWidth = 2

        self.layer.addSublayer(shapeLayer)
        
        shapeLayer.isHidden = true
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = 30.cgFloat
        
        let x = (self.bounds.width - w)/2.0
        
        let circleRect = CGRect.init(x: x, y: 3, width: w, height: w)
        
        let circlePath = UIBezierPath(roundedRect: circleRect, cornerRadius: w/2.0)
        
        shapeLayer.path = circlePath.cgPath
        
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var isSelected: Bool{
        didSet{
            
            shapeLayer.isHidden = !isSelected
            
            
        }
    }
    
}
